package com.ruoyi.business.controller;

import com.ruoyi.business.domain.CarMain;
import com.ruoyi.business.domain.Cardetail;
import com.ruoyi.business.dto.CardetailDto;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.service.ICarmainService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import java.util.Arrays;
import java.util.List;

/**
 * carmainController
 *
 * @author liutietou
 * @since 1.0 2025-07-15
 */
@RestController
@RequestMapping("/business/carmain")
public class CarmainController extends BaseController {
    @Autowired
    private ICarmainService carmainService;

    /**
     * 查询carmain列表
     */
    @PreAuthorize("@ss.hasPermi('business:carmain:list')")
    @GetMapping("/list")
    public TableDataInfo list(CarMain carmain) {
        Page<CarMain> page = carmainService.findCarmainPage(carmain);
        return getDataTable(page);
    }

    /**
     * 导出carmain列表
     */
    @PreAuthorize("@ss.hasPermi('business:carmain:export')")
    @Log(title = "carmain", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(CarMain carmain) {
        List<CarMain> list = carmainService.findCarmainList(carmain);
        ExcelUtil<CarMain> util = new ExcelUtil<>(CarMain.class);
        return util.exportExcel(list, "carmain");
    }

    /**
     * 获取carmain详细信息
     */
    @PreAuthorize("@ss.hasPermi('business:carmain:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(carmainService.findById(id));
    }

    /**
     * 新增carmain
     */
    @PreAuthorize("@ss.hasPermi('business:carmain:add')")
    @Log(title = "carmain", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CarMain carmain) {
        carmainService.save(carmain);
        return AjaxResult.success();
    }

    /**
     * 修改carmain
     */
    @PreAuthorize("@ss.hasPermi('business:cardetail:edit')")
    @Log(title = "carmain", businessType = BusinessType.UPDATE)
    @PostMapping("edit")
    public AjaxResult edit(@RequestBody List<CardetailDto> list) {
        carmainService.editCar(list);
        return AjaxResult.success();
    }

    /**
     * 删除carmain
     */
    @PreAuthorize("@ss.hasPermi('business:carmain:remove')")
    @Log(title = "carmain", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        carmainService.deleteByIds(Arrays.asList(ids));
        return AjaxResult.success();
    }
}
